package com.bjsxt.service.impl;


import com.bjsxt.mapper.AnswerOptDao;
import com.bjsxt.mapper.AnswerTxtDao;
import com.bjsxt.mapper.SurveyDao;
import com.bjsxt.pojo.Answer;
import com.bjsxt.pojo.AnswerOpt;
import com.bjsxt.pojo.Survey;
import com.bjsxt.service.SurveyServer;
import com.bjsxt.utils.BeanMapUtils;
import com.bjsxt.utils.MapParameter;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class SurveyServerImpl implements SurveyServer {

    @Autowired
    private SurveyDao surveyDao;

    @Autowired
    private AnswerOptDao answerOptDao;

    @Autowired
    private AnswerTxtDao answerTxtDao;



    @Override
    public int create(Survey survey) {

        return surveyDao.create(survey);
    }

    //根据id进行删除
    @Override
    public int delete(Integer id) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("id",id);
        Map<String, Object> map = MapParameter.getInstance().put("id", id).getMap();
        return surveyDao.delete(map);
    }

    @Override
    public int update(Survey survey) {
//        Map<String, Object> map = BeanMapUtils.beanToMapForUpdate(survey);
//        map.put("id",survey.getId());
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(survey)).getMap();
        map.put("id",survey.getId());
        System.out.println(map.toString());
        return surveyDao.update(map);
    }

    /**
     * deleteBatch 处理批量删除操作
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String ids) {
        int flag = 0;
        List<String> list = Splitter.on(",").splitToList(ids);
        for (String s:list){
            surveyDao.delete(MapParameter.getInstance().put("id",Integer.parseInt(s)).getMap());
            flag++;
        }
        return flag;
    }

    /**
     * 页面默认加载全部数据进行显示
     * @param survey
     * @return
     */
    @Override
    public List<Survey> query(Survey survey) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("id",survey.getId());
//        map.put("account",survey.getAccount());
//        map.put("password",survey.getPassword());
//        map.put("name",survey.getName());
//        map.put("phone",survey.getPhone());
//        map.put("remark",survey.getRemark());

        //使用pageHelper进行分页操作
        PageHelper.startPage(survey.getPage(),survey.getLimit());  //页数 当页总条数
        Map<String, Object> map = BeanMapUtils.beanToMap(survey);
        return surveyDao.query(map);
    }

    @Override
    public List<Survey> queryUrl(Survey survey) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("id",survey.getId());
//        map.put("account",survey.getAccount());
//        map.put("password",survey.getPassword());
//        map.put("name",survey.getName());
//        map.put("phone",survey.getPhone());
//        map.put("remark",survey.getRemark());

        //使用pageHelper进行分页操作
        PageHelper.startPage(survey.getPage(),survey.getLimit());  //页数 当页总条数
        Map<String, Object> map = BeanMapUtils.beanToMap(survey);
        return surveyDao.queryUrl(map);
    }


    /**
     * 修改数据前 查找选中行的全部数据 再进行赋值到修改页面
     * @param survey
     * @return
     */
    @Override
    public Survey detail(Survey survey) {

        return surveyDao.detail(BeanMapUtils.beanToMap(survey));
    }

    @Override
    public Survey detail(Integer id) {
        return surveyDao.detail(MapParameter.getInstance().put("id",id).getMap());
    }

    @Override
    public int count(Survey survey) {

        return surveyDao.count(BeanMapUtils.beanToMap(survey));
    }

    @Override
    public Survey login(String account, String password) {
        Map<String, Object> map = MapParameter.getInstance().put("account", account).put("password", password).getMap();
        return surveyDao.detail(map);
    }

    @Override
    public void updateState() {

        List<Integer> list = surveyDao.queryByState(Survey.state_create);
        for (Integer id : list) {
            surveyDao.update(MapParameter.getInstance().put("updateState",Survey.state_exec).put("id",id).getMap());
        }


        List<Integer> list2 = surveyDao.queryByStateForExec(Survey.state_exec);
        for (Integer id : list2) {
            surveyDao.update(MapParameter.getInstance().put("updateState",Survey.state_over).put("id",id).getMap());
        }
    }

    @Override
    public Integer submit(List<AnswerOpt> opts, List<Answer> answers) {

        int flag = 0;

        for (AnswerOpt opt : opts) {
            flag = answerOptDao.create(opt);
        }
        for (Answer answer : answers) {
            flag = answerTxtDao.create(answer);
        }
        return flag;
    }

    @Override
    public List<AnswerOpt> queryAnswer(AnswerOpt answerOpt) {

        List<AnswerOpt> optList = answerOptDao.query(MapParameter.getInstance().put(BeanMapUtils.beanToMap(answerOpt)).getMap());

        return optList;
    }
}
